package alf.mapping.view.touch;
import alf.mapping.dto.maps.Item;
import alf.mapping.view.Env;
import alf.mapping.view.activity.R;
import alf.mapping.view.visuals.EditorEngine;
import alf.mapping.view.visuals.Sprite;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.SystemClock;
import android.view.MotionEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TouchReactorManager
implements TouchReactor {

	EditorEngine editor = EditorEngine.getInstance();
	Context ctx;

	private TouchReactorCallback callback;
	private Map<EditorEngine.Mode,TouchReactor> mapa;
	
	public TouchReactorManager(Context ctx,TouchReactorCallback callback){
		this.ctx = ctx;
		this.callback = callback;
		mapa = new HashMap<EditorEngine.Mode,TouchReactor>();
	}

	public boolean onTouchEvent(MotionEvent event) {
		//posicion tocada
		boolean result = true;
		EditorEngine.Mode mode = editor.getMode();
		TouchReactor delegate = getDelegate(mode);
		return delegate.onTouchEvent(event);
	}

	private TouchReactor getDelegate(EditorEngine.Mode mode) {
		TouchReactor delegate = null;
		if (mapa.containsKey(mode)){
			delegate = mapa.get(mode);
		} else {
			delegate = createDelegate(mode);
			mapa.put(mode,delegate);
		}
		return delegate;
	}

	private TouchReactor createDelegate(EditorEngine.Mode mode) {
		TouchReactor r = null;
		if (mode==EditorEngine.Mode.NAVIGATE){
			r = new NavigatingTouchReactor();
		} else if (mode==EditorEngine.Mode.EDIT){
			r = new EditingTouchReactor();
		} else if (mode==EditorEngine.Mode.SELECTION){
			r = new SelectingTouchReactor();
		} else if (mode==EditorEngine.Mode.RESIZE){
			r = new ResizingTouchReactor();
		}
		if (r!=null){
			r.setCallback(this.callback);
			r.setCtx(ctx);
		}
		return r;
	}

	public void setCallback(TouchReactorCallback callback) {
		this.callback = callback;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}
	
}
